﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Biz
{
    public class ThongKeBiz
    {
        public List<HopDongView> ThongKeGiaTriHopDong()
        {
            using (var db = new HopDongEntities())
            {
                return db.HopDongViews.ToList();
            }
        }

        public List<HopDongView> ThongKeHopDongDangHoanCong()
        {
            using (var db = new HopDongEntities())
            {
                return db.HopDongViews.Where(x => x.TrangThai == (int)TrangThaiHopDong.DangHoanCong).ToList();
            }
        }

        public List<HopDongView> ThongKeHopDongDangQuyetToan()
        {
            using (var db = new HopDongEntities())
            {
                return db.HopDongViews.Where(x => x.TrangThai == (int)TrangThaiHopDong.DangQuyetToan).ToList();
            }
        }

        public List<ThongKeHopDongChiTietView> ThongKeHopDongChiTiet(DateTime? dtQuyetToanFrom, DateTime? dtQuyetToanTo)
        {
            //if (!dtQuyetToanFrom.HasValue && !dtQuyetToanTo.HasValue)
            //{
            //    return new List<ThongKeHopDongChiTietView>();
            //}

            using (var db = new HopDongEntities())
            {
                var query = from item in db.ThongKeHopDongChiTietViews select item;

                if (dtQuyetToanFrom.HasValue)
                {
                    query = query.Where(x => x.NgayQuyetToan.HasValue && x.NgayQuyetToan >= dtQuyetToanFrom);
                }

                if (dtQuyetToanTo.HasValue)
                {
                    query = query.Where(x => x.NgayQuyetToan.HasValue && x.NgayQuyetToan <= dtQuyetToanTo);
                }

                return query.ToList();
            }
        }

        public List<ThongKeHopDongChuaThanhToanView> ThongKeHopDongChuaThanhToan()
        {
            using (var db = new HopDongEntities())
            {
                return db.ThongKeHopDongChuaThanhToanViews.ToList();
            }
        }
    }
}
